<?php
/**
 * The order class.
 * @author Ian Dunn <scholar42@gmail.com>
 * @version 1.0
 * @since 2013-11-03
 */
require_once 'database.php';
require_once 'table.php';
require_once 'phase.php';

class order extends table {

	static $priorities = array ("", "Highest", "High", "Normal", "Low", "Lowest");

	var $deadline;
	var $customer;
	var $priority = 3;
	var $shipping_addr;
	var $name;
	var $quantity;
	var $completion_date;
	var $special_instructions;
	var $hold = false;
	var $purchase_number;
	var $weighted;
	var $start_time;
	var $stop_time;
	var $price;

	/**
	 * The constructor of the order class.
	 *
	 * @param i	 The ID of this order.
	 * @return	 The newly initialized order object.
	 */
	function __construct($i) {
		parent::__construct ($i, "Orders");
	}

	/**
	 * Mark this job as completed.
	 *
	 * @param $db	A database object that is connected to the database.
	 */
	function complete ($db = NULL) {
		if (!$db)
			$db = new database ();

		$this->completion_date = time ();
		$result = $this->update_database_by_id ($db);
		if (!$result) {
			echo $db->error() . "<br>\n";
			echo "You do not have the correct permissions to perform this action.<br>\n";
			return;
		}
	}

	/**
	 * Place a hold on this order.
	 *
	 * @param db	A database object.
	 * @return		Whether or not the hold was successful.
	 */
	function place_hold ($db = NULL) {
		if (!$db)
			$db = new database ();

		if ($this->hold)
			return false;
		$this->hold = true;

		$result = $this->update_database_by_id ($db);
		if (!$result) {
			echo "You do not have the correct permissions to perform this action.<br>\n";
			return false;
		}
		$result->free ();
		return true;
	}

	/**
	 * Remove a hold from this order.
	 *
	 * @param db
	 * @return		Whether or not the operation was successful.
	 */
	function remove_hold ($db) {
		if (!$db)
			$db = new database ();

		if (!$this->hold)
			return false;
		$this->hold = false;

		$result = $this->update_database_by_id ($db);
		if (!$result) {
			echo "You do not have the correct permissions to perform this action.<br>\n";
			return false;
		}
		$result->free ();
		return true;
	}

	/**
	 * Determine the phases of this order.
	 *
	 * @param db	A database object.
	 * @return		The number of phases for this order.
	 */
	function num_phases ($db = NULL) {
		if (!$db)
			$db = new database ();

		$cols = array ("id");
		$where = sprintf("ordId=%d", $this->id);
		$result = $db->select_entry ("Phases", $cols, $where);
		$ret = $result->num_rows;
		$result->free ();
		return $ret;
	}

	/**
	 * Collect an array of phases.
	 *
	 * @param db	A database object.
	 * @return		An array of phase objects for this order, ordered by priority.
	 */
	function collect_phases ($db = NULL) {
		if (!$db)
			$db = new database ();

		$cols = array ("id");
		$where = sprintf("ordId=%d", $this->id);
		$result = $db->select_entry ("Phases", $cols, $where);
		$ret = array ();
		for ($i = 0; $i < $result->num_rows; $i++) {
			$arr = $result->fetch_row ();
			$id = $arr[0];
			$phase = new phase ((int)$id);
			$phase->init ($db);
			$ret[(int)$phase->priority] = $phase;
		}
		$result->free ();
		return $ret;
	}

	/**
	 * Creates a maintenance 'order'.
	 * Opol handles maintenance by scheduling it as an order.
	 *
	 * @param machId	The id of the machine.
	 * @param db		A database object, or NULL for the default.
	 */
	static function create_maint_order ($machId, $db = NULL) {
		if (!$db)
			$db = new database ();

		$mach = new machine($machId);
		$mach->init($db);

		// Determine id.
		$ordId = 0;
		$order = new order ($ordId);

		$order->special_instructions = sprintf ("MaintOrder");
		//TODO: Use the Timeapprox table to determine the maintenance length.
		$order->deadline = (int)$mach->last_maintenance + (int)$mach->time_between_maintenances + 120;
		$order->priority = 3;
		$result = $order->insert_to_database($db);
		echo $db->error() . "<br>\n";

		$phaseId = 0;
		$phase = new phase ((int)$phaseId);
		$phase->machId = $machId;
		$phase->ordId = (int)$order->id;
		$phase->priority = 0;
		$result = $phase->insert_to_database($db);
		echo $db->error() . "<br>\n";
	}

	/**
	 * Correctly updates an order in the database.
	 */
	function update_database_by_id ($db = NULL) {
		if (!$db)
			$db = new database ();

		$cols = array (
			"customer" => "'$this->customer'",
			"shipping_addr" => "'" . $db->sanitize_data ($this->shipping_addr) . "'",
			"hold" => $this->hold ? "1" : "0",
			"quantity" => "'$this->quantity'",
			"priority" => "'$this->priority'",
			"price" => "'$this->price'",
			"special_instructions" => "'" . $db->sanitize_data ($this->special_instructions) . "'",
			"deadline" => "'$this->deadline'"
		);

		$result = $db->update_entry_by_id ("Orders", $cols, $this->id);
		return $result;
	}

	/**
	 * Correctly inserts an order in the database.
	 */
	function insert_to_database ($db = NULL) {
		if (!$db)
			$db = new database ();

		$cols = array (
			"customer" => "'$this->customer'",
			"shipping_addr" => "'$this->shipping_addr'",
			"hold" => $this->hold ? "1" : "0",
			"quantity" => "'$this->quantity'",
			"priority" => "'$this->priority'",
			"price" => "'$this->price'",
			"special_instructions" => "'$this->special_instructions'",
			"deadline" => "'$this->deadline'"
		);

		$result = $db->insert_entry ("Orders", $cols);
		if ($result)
			$this->id = $db->insert_id ();
		return $result;
	}

	/**
	 * Determines whether or not this order is a maintenance order.
	 */
	function maint_order () {
		return !strcmp ($this->special_instructions, "MaintOrder");
	}
}
?>
